package com.example.guli.service.ucenter.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.guli.common.base.result.ResultCodeEnum;
import com.example.guli.common.base.util.jwt.JwtInfo;
import com.example.guli.common.base.util.jwt.JwtUtils;
import com.example.guli.common.base.util.sms.FormUtils;
import com.example.guli.common.base.util.sms.MD5;
import com.example.guli.service.base.DTO.MemberDto;
import com.example.guli.service.base.exception.GuliException;
import com.example.guli.service.ucenter.entity.Member;
import com.example.guli.service.ucenter.entity.vo.LoginVo;
import com.example.guli.service.ucenter.entity.vo.RegisterVo;
import com.example.guli.service.ucenter.mapper.MemberMapper;
import com.example.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author shunxuan
 * @since 2020-09-09
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    /**
     * 会员注册
     * @param registerVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if (
                StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(nickname)) {
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }
        //校验验证码：从redis中取值
        //暂时还没有redis，有了之后再测
        //String checkCode = (String)redisTemplate.opsForValue().get(mobile);
        String checkCode="2233";
        if(!code.equals(checkCode)){
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }
        //判断用户是否注册：从MySQL中取值
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new GuliException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }
        //注册
        Member member = new Member();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("https://gili-file-0717.oss-cn-beijing.aliyuncs.com/avatar/default_handsome.jpg");
        member.setDisabled(false);
        baseMapper.insert(member);
    }

    @Override
    public String login(LoginVo loginVo) {
        //校验：参数是否合法 手机号是否存在于数据库 校验用户是否被禁用setDisabled
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //校验参数是否合法
        if(StringUtils.isEmpty(mobile)||!FormUtils.isMobile(mobile)
            ||StringUtils.isEmpty(password)
        ){
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Member member = baseMapper.selectOne(queryWrapper);
        if(member==null){//校验手机号是否存在于数据库
            throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
        }
        if(!MD5.encrypt(password).equals(member.getPassword())){//校验密码是否正确
            throw new GuliException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        if(member.getDisabled()){//校验用户是否被禁用setDisabled
            throw new GuliException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }
        //登录：生成token
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());
        //设置token过期时间
        String jwtToken = JwtUtils.getJwtToken(jwtInfo, 43200000);
        return jwtToken;

    }

    @Override
    public Member getByOpenid(String openid) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public MemberDto getMemberDtoByMemberId(String memberId) {
        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member,memberDto);
        return memberDto;
    }

    @Override
    public Integer countReisterNum(String day) {
       return baseMapper.selectRegisterNumByDay(day);
    }


}
